import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

import CustomSprite;
import Button;
import Images;


class ActionMenuEvent extends Event{
	public function new(customEventString:String){

	super(customEventString, true, false);

    }
}

class ActionMenu extends GenericMenu
{
public var currentPlayer:CharacterStat;
public var HostEvent:CustomSprite;
public var HostEventText:TextField;
public var Propaganda:CustomSprite;
public var PropagandaText:TextField;
public var ExpandCastle:CustomSprite;
public var ExpandCastleText:TextField;
public var Description:CustomSprite;
public var DescriptionText:TextField;
     public function new(inBackground:BitmapData,inPlayer:CharacterStat) {
     super(inBackground,640,420);
     y = 100;
     currentPlayer = inPlayer;
     HostEvent = new CustomSprite();
     HostEvent.graphics.beginFill(0xFFFFFF);
     HostEvent.graphics.lineStyle(1);
     HostEvent.graphics.drawRect(0,0,100,100);
     HostEvent.graphics.endFill();
     HostEvent.graphics.beginBitmapFill(new EventImage());
     HostEvent.graphics.drawRect(0, 0, 100,100);
     HostEvent.graphics.endFill();
     HostEvent.isHover = false;
     HostEvent.setHoverText("");
     HostEvent.x =20;
     HostEvent.y = 85;
     HostEvent.buttonMode =true; 
      
     HostEventText = new TextField();
     HostEventText.htmlText = "<font size='20'>Event</font>";
     HostEventText.x = 125;
     HostEventText.y = 78;
     HostEventText.autoSize = TextFieldAutoSize.LEFT;
     HostEvent.addEventListener(MouseEvent.CLICK, onHostEventButtonClick);
     HostEvent.addEventListener(MouseEvent.ROLL_OVER,onHoverEvent);
     HostEvent.addEventListener(MouseEvent.ROLL_OUT,onHoverOutResource);
    

     Propaganda = new CustomSprite();
     Propaganda.graphics.beginFill(0xFFFFFF);
     Propaganda.graphics.lineStyle(1);
     Propaganda.graphics.drawRect(0,0,100,100);
     Propaganda.graphics.endFill();
     Propaganda.graphics.beginBitmapFill(new PropagandaImage());
     Propaganda.graphics.drawRect(0, 0, 100,100);
     Propaganda.graphics.endFill();
     Propaganda.isHover = false;
     Propaganda.setHoverText("");
     Propaganda.x =210;
     Propaganda.y = 85;
     Propaganda.buttonMode =true;

     PropagandaText = new TextField();
     PropagandaText.htmlText = getPropagandaDesc();
     PropagandaText.x = 315;
     PropagandaText.y = 78;
     PropagandaText.autoSize = TextFieldAutoSize.LEFT;
     Propaganda.addEventListener(MouseEvent.CLICK, onPropagandaButtonClick);
     Propaganda.addEventListener(MouseEvent.ROLL_OVER,onHoverPropaganda);
     Propaganda.addEventListener(MouseEvent.ROLL_OUT,onHoverOutResource);


     ExpandCastle = new CustomSprite();
     ExpandCastle.graphics.beginFill(0xFFFFFF);
     ExpandCastle.graphics.lineStyle(1);
     ExpandCastle.graphics.drawRect(0,0,100,100);
     ExpandCastle.graphics.endFill();
     ExpandCastle.buttonMode =true;
  
     var ExpandCastlePic = new CustomSprite();
     ExpandCastlePic.graphics.beginBitmapFill(new CastleSmallImage());
     ExpandCastlePic.graphics.drawRect(0, 0, 100,100);
     ExpandCastlePic.graphics.endFill();
     ExpandCastlePic.resize(90,90);
     ExpandCastle.addChild(ExpandCastlePic);
     ExpandCastlePic.x=5;ExpandCastlePic.y=10;

     ExpandCastle.isHover = false;
     ExpandCastle.setHoverText("");
     ExpandCastle.x = 435;
     ExpandCastle.y = 85;
    
     ExpandCastleText = new TextField();
     ExpandCastleText.htmlText = getExpandCastleText();
     ExpandCastleText.x = 540;
     ExpandCastleText.y = 78;
     ExpandCastleText.autoSize = TextFieldAutoSize.LEFT;
     ExpandCastle.addEventListener(MouseEvent.CLICK, onExpandCastleButtonClick);
     ExpandCastle.addEventListener(MouseEvent.ROLL_OVER,onHoverCastle);
     ExpandCastle.addEventListener(MouseEvent.ROLL_OUT,onHoverOutResource);

     Description = new CustomSprite();
     Description.graphics.beginFill(0xFFFFFF);
     Description.graphics.lineStyle(1);
     Description.graphics.drawRect(0,0,410,205);
     Description.graphics.endFill();
     Description.x =210;
     Description.y =200;
     
      
     DescriptionText = new TextField();
     DescriptionText.htmlText = "<font size='20'></font>";
     DescriptionText.x = 5;
     DescriptionText.y = 5;
     DescriptionText.width = 405;
     DescriptionText.wordWrap= true;
     DescriptionText.selectable = false;
     DescriptionText.autoSize = TextFieldAutoSize.LEFT;
     Description.addChild(DescriptionText);
     addChild(Description);

     addChild(ExpandCastleText);
     addChild(ExpandCastle);

     addChild(PropagandaText);
     addChild(Propaganda);

     addChild(HostEventText);
     addChild(HostEvent);
  }

 private function onHoverEvent(event:MouseEvent)
  {
    DescriptionText.htmlText = getEventText();
  }
 private function onHoverPropaganda(event:MouseEvent)
  {
    DescriptionText.htmlText = getPropagandaText();
  }
 private function onHoverCastle(event:MouseEvent)
  {
    DescriptionText.htmlText = getCastleText();
  }
  private function onHoverOutResource(event:MouseEvent)
  {	
    DescriptionText.htmlText = "<font size='20'></font>";
  }

  private function getPopulationFlavorText():String
  {
    var text = "Try to take over ";
    if(currentPlayer.PopulationLevel <= 0)
    {
	text +="neighboring villages";
    }
    else if(currentPlayer.PopulationLevel == 1)
    {
	text +="the country";
    }
    else if(currentPlayer.PopulationLevel == 2)
    {
	text +="small empire";
    }
    else if(currentPlayer.PopulationLevel == 3)
    {
	text +="large empire";
    }
    else if(currentPlayer.PopulationLevel == 4)
    {
	text +="<b>The World!</b>";
    }
    else if(currentPlayer.PopulationLevel == 5)
    {
      text +="The Universe?";
    }
    else
    {
       text +="Level " + (currentPlayer.PopulationLevel+2);
    }
    return text;
  }
  private function getExpandCastleText():String
  {
    return "<font size='20'>Expand\nCastle</font>\n<font size='16'>Level: "+  (currentPlayer.PopulationLevel+1) + "</font>";
  }

  private function getPropagandaDesc():String
  {
    return "<font size='20'>Propaganda</font>\n<font size='16'>Suspicion: "+  HelperMethods.format(currentPlayer.getSuspicionPerTurn(),2) + "\nPer Frame</font>";
  }

  private function getEventText():String
  {
     var text = "<font size = '20'><b><u>Fear - " + currentPlayer.getHostEventCost() + "</u></b>\nHost go-kart racing,tennis,parties,fighting tournament, or what ever it takes to decrease suspicion.</font>";
     return text;
  }
  private function getPropagandaText():String
  {
     var text = "<font size = '20'><b><u>Fear - " + currentPlayer.getPropagandaCost() + "</u></b>\nNothing like acquiring some newspaper and schools, to educate the public properly. Decreases suspicion a small percent each turn.</font>";
     return text;
  }
  private function getCastleText():String
  {
     var text = "<font size = '20'><b><u>Fear - " + currentPlayer.getExpandCastleCost() + "</u></b>\nWith enough fear, you'll be able to mobilize your army and expand your empire.\n\n"+getPopulationFlavorText()+"</font>";
     return text;
  }
  private function onHostEventButtonClick(event:MouseEvent) {
      var cost = currentPlayer.getHostEventCost();
      if(currentPlayer.Fear >= cost)
      {
	currentPlayer.HostedEvents++;
	currentPlayer.Suspicion -=1000;
	currentPlayer.Fear -=cost;
      }
      DescriptionText.htmlText = getEventText();
  }

  private function onPropagandaButtonClick(event:MouseEvent) {
      var cost = currentPlayer.getPropagandaCost();
      if(currentPlayer.Fear >= cost)
      {
	currentPlayer.Propaganda +=1;
	currentPlayer.Fear -= cost;
      }
      DescriptionText.htmlText = getPropagandaText();
      PropagandaText.htmlText = getPropagandaDesc();
  }

  private function onExpandCastleButtonClick(event:MouseEvent) {
      var cost = currentPlayer.getExpandCastleCost();
      if(currentPlayer.Fear >= cost)
      {
	currentPlayer.PopulationLevel +=1;
	currentPlayer.Fear -= cost;
	currentPlayer.MaxFear = 10000 * Math.pow(2,currentPlayer.PopulationLevel);
	dispatchEvent(new Event("ExpandedCastle"));
      }
      DescriptionText.htmlText = getCastleText();
      ExpandCastleText.htmlText = getExpandCastleText();
      PropagandaText.htmlText = getPropagandaDesc();
  }
}